package persistency.reader;

/**
 * CourseReader.java
 * Purpose: this class represents a course that students can take
 * 
 * @author Hans Brems
 * @version 1.0 05/11/2011
 */

import java.util.ArrayList;

import model.Course;
import model.Teacher;

import model.container.CourseContainer;
import model.container.TeacherContainer;

public class TeacherPreferenceReader extends ContentReader {
	
	/** Constructs a course reader object **/
	public TeacherPreferenceReader() {
	}
	
	public TeacherPreferenceReader(String filePath) {
		super(filePath);
	}
	
	/** Reads a text file containing course objects into a collection */
	public ArrayList<Teacher> read() {
		
		// Create list
		ArrayList<Teacher> teachers= new ArrayList<Teacher>();
		
		// Skip the first line (header)
		this.scanner.nextLine();

		// Construct a teacher object from each line
		while (this.scanner.hasNext()) {
			
			// Create a new teacher
			String teacherNumber = this.scanner.next();
			Teacher teacher = TeacherContainer.getInstance().getTeacherByID(teacherNumber);
			int coursePreference = Integer.parseInt(this.scanner.next());
			Course preferredCourse  = CourseContainer.getInstance().getCourseByID(coursePreference);
			
			// Add required course as requirement
			try
			{
				teacher.addPreferredCourse(preferredCourse);
				
			}
			catch (Exception ex)
			{
				System.out.println("Cannot add required course: " + ex.getMessage());
			}
			
			// Add course to collection
			teachers.add(teacher);
		}
		
		// Return list
		return teachers;
	}
}